package com.sky.zzh.adapter;

import java.util.HashMap;

import org.json.JSONArray;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.sky.zzh.R;

public class TimeAdapter extends BaseAdapter {

	private Context mContext;
	private JSONArray jsonArray;
    HashMap<Integer,View> lmap = new HashMap<Integer,View>(); 
    private holder h;
	public TimeAdapter(Context mContext,JSONArray jsonArray) {
		super();
		this.mContext = mContext;
		this.jsonArray = jsonArray;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return jsonArray.length();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public View getView(final int position, View arg1, ViewGroup arg2) {
		// TODO Auto-generated method stub
		if(lmap.get(position) == null){
			arg1 = LayoutInflater.from(mContext).inflate(R.layout.item_1, null);
			h = new holder();
			h.tvLeftDate = (TextView) arg1.findViewById(R.id.tv_left_date);
			h.tvLeftTime = (TextView) arg1.findViewById(R.id.tv_left_time);
			h.tvRightDate = (TextView) arg1.findViewById(R.id.tv_right_date);
			h.tvRightTime = (TextView) arg1.findViewById(R.id.tv_right_time);
			lmap.put(position, arg1);
			arg1.setTag(h);
		}else{
			arg1 = lmap.get(position);
			h = (holder) arg1.getTag();
		}
		
		if(position % 2 == 0){
			h.tvLeftDate.setText(jsonArray.optJSONObject(position).optString("At").substring(5, 7)+"月"+jsonArray.optJSONObject(position).optString("At").substring(8, 10)+"日");
			h.tvLeftTime.setText(jsonArray.optJSONObject(position).optString("At").substring(11));
		}else{
			h.tvRightDate.setText(jsonArray.optJSONObject(position).optString("At").substring(5, 7)+"月"+jsonArray.optJSONObject(position).optString("At").substring(8, 10)+"日");
			h.tvRightTime.setText(jsonArray.optJSONObject(position).optString("At").substring(11));
		}
		
		return arg1;
	}
	  
	static class holder{
		TextView tvLeftDate,tvLeftTime,tvRightDate,tvRightTime;
	}

}
